package com.example.fresh_market.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.bumptech.glide.request.RequestOptions;
import com.example.fresh_market.databinding.ActivityMainAdminBinding;
import com.example.fresh_market.entity.UserInfo;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.lang3.StringUtils;

public class MainAdminActivity extends AppCompatActivity {
    private ActivityMainAdminBinding binding;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainAdminBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = this;
        init();
        bindClickListener();
    }

    private void bindClickListener() {
        binding.admBtnOrder.setOnClickListener(v -> {
            startActivity(new Intent(mContext, OrderManageActivity.class));
        });
        binding.admBtnProd.setOnClickListener(v -> {
            startActivity(new Intent(mContext, ProductActivity.class));
        });
        binding.admBtnCategory.setOnClickListener(v -> {
            startActivity(new Intent(mContext, CategoryActivity.class));
        });
        binding.admBtnPersonal.setOnClickListener(v -> {
            startActivity(new Intent(mContext, AccountSettingActivity.class));
        });
    }

    public void init() {
        HttpUtil.get(mContext, "/user", response -> {
            ApiResponse<UserInfo> apiResponse = new Gson().fromJson(response, new TypeToken<ApiResponse<UserInfo>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                UserInfo userInfo = apiResponse.getData();
                binding.userName.setText(String.format("用户名：%s", userInfo.getNickname()));
                binding.userEmail.setText(String.format("邮箱：%s", userInfo.getEmail()));
                binding.userPhone.setText(String.format("电话：%s", userInfo.getPhone()));
                if (StringUtils.isNotBlank(userInfo.getAvatar())) {
                    HttpUtil.loadImage(binding.userAvatar, userInfo.getAvatar(), new RequestOptions());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }
}
